import org.devops.git
import org.devops.dingmes
import org.devops.mvnbuild
import org.devops.dockerbuild
import org.devops.deploy
import org.devops.vue

def call(Map map){
    def private g = new git()
    // def private mb = new mvnbuild()
    def private dm = new dingmes()
    // def private dockerb = new dockerbuild()
    def private vueb = new vue()
    def private dp = new deploy()
    //def image_name = map.registry+'/'+map.image_name+'/'+map.project_name+':'+params.ENV_TYPE+'-'+env.BUILD_ID
    def server_path = map.vue_server_path+'/'+params.ENV_TYPE+'/'+map.project_name
    def ansible_host = map.ansible_host+'-'+params.ENV_TYPE
    def vue_k8sserver_path = map.vue_k8sserver_path+'/'+params.ENV_TYPE+'/'+map.project_name
    pipeline {
        agent {
            docker {
                image 'registry-vpc.cn-beijing.aliyuncs.com/bangying/open:node20' // 镜像中包含ansible
                args '-v /data/jenkins/m2:/root/.m2 --label JenkinsAgent=true' 

                // 当设置reuseNode设置为true时：不会创建新的工作区，当前代理的当前工作区将被装入容器，且将在同一节点上启动该容器，所以整体数据将被同步
                reuseNode true
            }
        }
        parameters {
            string(name: 'PROJECT_NAME',
                defaultValue:  map.project_name,
                description: '项目名称')

            choice(name: 'ENV_TYPE', 
                choices: ['dev', 'pro'], 
                description: '部署环境：dev:开发环境;  pro: 生产环境')

            string(name: 'PROJECT_URL',
                defaultValue: map.git_url, 
                description: '项目仓库地址')

            gitParameter (
                    defaultValue: map.git_default_branch,
                    branchFilter: 'origin/(.*)',
                    name: 'BRANCH',
                    quickFilterEnabled: true,
                    selectedValue: 'DEFAULT',
                    sortMode: 'DESCENDING_SMART',
                    tagFilter: '*',
                    type: 'PT_BRANCH',
                    description: 'Please select a branch or tag to build',
                    useRepository: map.git_url
            )
            choice(name: 'VUE_PARM', 
                choices: ['npm run build', 'npm run build:prod', 'npm run build:test', 'npm run build:dev', 'npm  run generate'], 
                description: 'node构建参数')
        }

        stages{
            stage('git clone') {
                steps{
                    // cleanWs()
                    script{
                        BRANCH_TAG = sh(returnStdout: true,script: "echo ${params.BRANCH_TAG}|awk -F '/' '{print \$NF}'").trim()
                        g.gitco(params.BRANCH, map.git_credentials_id, map.git_url)
                    }
                }
            }

            stage('node build') {
                steps {
                        script{
                            echo "[INFO] 构建命令: $map.nodecmd"
                            vueb.nbuild(map.nodecmd,map.change_profile_dir,map.git_profile_path,params.VUE_PARM)
                        }
                        stash includes: 'dist/*', name: BUILD_NUMBER
                }
            } 

            stage('deploy'){
                parallel {
                    stage('deploy dev'){
                        // 测试环境部署
                        when {
                            expression { params.ENV_TYPE ==~ /(dev)/ }  
                            // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script{
                                echo "[INFO] dev构建方式: $map.dev_deploy"
                                if ( map.dev_deploy == 'k8s' ) {
                                    dp.k8sVueDeploy(map.k8s_conn_yaml, vue_k8sserver_path, map.k8s_namespace)
                                }
                                if ( map.dev_deploy == 'ansible' ) {
                         
                                        dp.ansibleVueDeploy(map.ansible_hosts_file, ansible_host, server_path, map.git_profile_path)  
                                    
                                }
                                if ( map.dev_deploy == 'ssh' ) {
                                    dp.sshVueDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, server_path)  
                                }
                                if ( map.dev_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }  
                            }
                        }
                    }
                    stage('deploy test'){
                        // 测试环境部署
                        when {
                            expression { params.ENV_TYPE ==~ /(test)/ }  
                            // expression { BRANCH_TAG ==~ /(test)/ }
                        }
                        steps {
                            script{
                                echo "[INFO] test构建方式: $map.test_deploy $map.ansible_hosts_file, $ansible_host, $server_path, $map.git_profile_path"
                                if ( map.test_deploy == 'k8s' ) {
                                    dp.k8sVueDeploy(map.k8s_conn_yaml, vue_k8sserver_path, map.k8s_namespace)
                                }
                                if ( map.test_deploy == 'ansible' ) {
                                  
                                        dp.ansibleVueDeploy(map.ansible_hosts_file, ansible_host, server_path, map.git_profile_path)  
                                    
                                }
                                if ( map.test_deploy == 'ssh' ) {
                                    dp.sshVueDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, server_path)  
                                }
                                if ( map.test_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }  
                            }
                        }
                    }
                    stage('deploy pre'){
                        // 预发布环境部署
                        when {
                            expression { params.ENV_TYPE ==~ /(pre)/ }  
                            // expression { BRANCH_TAG ==~ /(pre2)/ }
                        }
                        steps {
                            script{
                                echo "[INFO] pre构建方式: $map.pre_deploy"
                                if ( map.pre_deploy == 'k8s' ) {
                                    dp.k8sVueDeploy(map.k8s_conn_yaml, vue_k8sserver_path, map.k8s_namespace)
                                }
                                if ( map.pre_deploy == 'ansible' ) {
                                  
                                        dp.ansibleVueDeploy(map.ansible_hosts_file, ansible_host, server_path, map.git_profile_path)  
                                    
                                }
                                if ( map.pre_deploy == 'ssh' ) {
                                    dp.sshVueDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, server_path)  
                                }
                                if ( map.pre_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }
                            }
                        }
                    }
                    
                    stage('deploy pro'){
                        when {
                            allOf {
                                expression { params.ENV_TYPE ==~ /(pro)/ }           
                            }
                            anyOf {
                                expression { env.BUILD_USER == "admin" }
                                expression { env.BUILD_USER == "ZhangYu" }
                                expression { env.BUILD_USER == "jiangxiliang" }
                            }
                        }
                        steps {
                            script{
                                echo "[INFO] pro构建方式: $map.pro_deploy"
                                if ( map.pro_deploy == 'k8s' ) {
                                    dp.k8sVueDeploy(map.k8s_conn_yaml, vue_k8sserver_path, map.k8s_namespace)
                                }
                                if ( map.pro_deploy == 'ansible' ) {
                             
                                        dp.ansibleVueDeploy(map.ansible_hosts_file, ansible_host, server_path, map.git_profile_path)  
                                    
                                }
                                if ( map.pro_deploy == 'ssh' ) {
                                    dp.sshVueDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, server_path)  
                                }
                                if ( map.pro_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }
                            }
                        }
                    }
                    stage('deploy bypro'){
                        when {
                            allOf {
                                expression { params.ENV_TYPE ==~ /(bypro)/ }           
                            }
                            anyOf {
                                expression { env.BUILD_USER == "admin" }
                                expression { env.BUILD_USER == "zhangyu" }
                                expression { env.BUILD_USER == "jiangxiliang" }
                            }
                        }
                        steps {
                            script{
                                echo "[INFO] pro构建方式: $map.pro_deploy"
                                if ( map.pro_deploy == 'k8s' ) {
                                    dp.k8sVueDeploy(map.k8s_conn_yaml, vue_k8sserver_path, map.k8s_namespace)
                                }
                                if ( map.pro_deploy == 'ansible' ) {
                            
                                        dp.ansibleVueDeploy(map.ansible_hosts_file, ansible_host, server_path, map.git_profile_path)  
                                    
                                }
                                if ( map.pro_deploy == 'ssh' ) {
                                    dp.sshVueDeploy(map.ssh_name, map.ssh_host, map.ssh_port, map.ssh_cred, server_path)  
                                }
                                if ( map.pro_deploy == 'nodeploy' ) {
                                    throw new Exception("您选择的环境未配置部署方式！！！,请联系运维人员处理")
                                }
                            }
                        }
                    }
                }
            }         
       }
        post {
            // success {
            //     script {
            //         dm.DingdingReq(map.robotId, "构建成功 ✅")
            //     }
            // }
            failure {
                script {
                    dm.DingdingReq(map.robotId, "构建失败 ❌")
                }
            }
        }
    }
}

